package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.MovieBackMapper;
import com.zretc.entity.Movie;
import com.zretc.service.MovieBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiaozhiyu
 * @date 2020-7-28
 */
@Service
public class MovieBackServiceImpl implements MovieBackService {

    @Autowired
    private MovieBackMapper movieBackMapper;

    @Override
    public boolean deleteMovie(String movieId, String adminId) {

        // 调用方法查询影院id
        Map<String, String> map = movieBackMapper.findCinemaByAdminId(adminId);
        // 获取id
        String cinemaId = map.get("cinemaId");
        // 调用影院id和电影id删除电影-下架
        int deleteMovieRelation = movieBackMapper.deleteMovieRelation(movieId, cinemaId);

        if(deleteMovieRelation != 0){
            return true;
        } else{
            return false;
        }

    }

    @Override
    public PageInfo<Movie> getMovieByAdminIdAndMovieName(Map<String, String> params) {

        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取管理员id
        String adminId = params.get("adminId");
        // 获取电影名
        String movieName = params.get("movieName");

        // 1.开始分页
        PageHelper.startPage(pageNum,pageSize);

        // 调用dao方法
        // 对传递给dao方法中参数进行处理
        Map<String,String> map = new HashMap<>();

        if ("".equals(movieName) || movieName == null){
            map.put("movieName",null);
        }else{
            map.put("movieName","%" + movieName + "%");
        }

        if ("".equals(adminId) || adminId == null){
            map.put("adminId",null);
        }else{
            map.put("adminId", adminId);
        }

        // 调用方法
        List<Movie> movieList = movieBackMapper.findMovieByAdminIdAndMovieName(map);

        // 3.包装到PageInfo对象
        PageInfo<Movie> PageInfo = new PageInfo<>(movieList);

        return PageInfo;
    }

    @Override
    public PageInfo<Movie> getMovieByAdminId(Map<String, String> params) {

        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取管理员id
        String adminId = params.get("adminId");

        // 1.开始分页
        PageHelper.startPage(pageNum,pageSize);

        // 调用dao方法
        List<Movie> movieByAdminId = movieBackMapper.findMovieByAdminId(adminId);

        // 3.包装到PageInfo对象
        PageInfo<Movie> PageInfo = new PageInfo<>(movieByAdminId);

        return PageInfo;
    }
}
